//
// Created by subject on 2025/2/25.
//

#include "JavaSimpleManager.h"

static jobject g_dsManagerInstance = nullptr;
static jmethodID g_dsGetResultSimple = nullptr;
static jmethodID g_dsFinish = nullptr;

jclass JavaSimpleManager::findClass(JNIEnv *env, const char *className) {
    jclass clazz = env->FindClass(className);
    if (clazz == nullptr) {
        LOGE("Failed to find class: %s", className);
    }
    return clazz;
}

jmethodID JavaSimpleManager::getMethodID(JNIEnv *env, jclass clazz, const char *methodName,
                                         const char *methodSig) {
    jmethodID methodID = env->GetMethodID(clazz, methodName, methodSig);
    if (methodID == nullptr) {
        LOGE("Failed to find method: %s with signature: %s", methodName, methodSig);
    }
    return methodID;
}

jmethodID JavaSimpleManager::getStaticMethodID(JNIEnv *env, jclass clazz, const char *methodName,
                                               const char *methodSig) {
    jmethodID methodID = env->GetStaticMethodID(clazz, methodName, methodSig);
    if (methodID == nullptr) {
        LOGE("Failed to find static method: %s with signature: %s", methodName, methodSig);
    }
    return methodID;
}

bool JavaSimpleManager::initJavaSimple(JNIEnv *env) {
//    1. 获取 DsManager 类的引用
    jclass dsManagerClass = findClass(env, "com/taiic/deepseek/DsManager");
    if(dsManagerClass == nullptr)
        return false;

//    2. 获取 DsManager 的 GetInstance 方法ID
    jmethodID dsGetInstance = getStaticMethodID(env, dsManagerClass, "GetInstance", "()Lcom/taiic/deepseek/DsManager;");
    if(dsGetInstance == nullptr)
        return false;

//    3.1 获取 onGetResultSimple 的方法ID
    g_dsGetResultSimple = getMethodID(env, dsManagerClass, "onGetResultSimple", "(Ljava/lang/String;I)V");
    if(g_dsGetResultSimple == nullptr)
        return false;
//    3.2 获取 onFinsh 的方法ID
    g_dsFinish = getMethodID(env, dsManagerClass, "onFinsh", "()V");
    if(g_dsFinish == nullptr)
        return false;

//    4. 调用 getInstance 方法， 获取 DsManager 的单例实例
    jobject dsManagerInstance = env->CallStaticObjectMethod(dsManagerClass, dsGetInstance);
    if(dsManagerInstance == nullptr)
        return false;

//    将局部引用转化为全局引用
    g_dsManagerInstance = env->NewGlobalRef(dsManagerInstance);
    if(g_dsManagerInstance == nullptr)
        return false;

//    删除局部引用，放止内存泄露
    env->DeleteLocalRef(dsManagerClass);
    env->DeleteLocalRef(dsManagerInstance);

    return true;
}

void JavaSimpleManager::destoryJaveSimple(JNIEnv *env) {
//    6. 删除全局引用，防止内存泄露
    env->DeleteGlobalRef(g_dsManagerInstance);

}

bool JavaSimpleManager::callJavaSimple(JNIEnv *env, const char *text, const int state) {
    if(g_dsManagerInstance == nullptr || g_dsGetResultSimple == nullptr){
        LOGE("DsManager instance or onGetResultSimple is null\n");
        return false;
    }

// 动态分配内存
    char* destination = new char[strlen(text) + 1]; // +1 是为了存储 '\0'
    strcpy(destination, text);
    jstring  jText = env->NewStringUTF(destination);
    delete[] destination;
//    5.1 调用 onGetResultSimple 方法
    env->CallVoidMethod(g_dsManagerInstance, g_dsGetResultSimple, jText, state);

    env->DeleteLocalRef(jText);
    return true;
}

bool JavaSimpleManager::callJavaFinish(JNIEnv *env) {
    if(g_dsManagerInstance == nullptr || g_dsFinish == nullptr){
        LOGE("DsManager instance or onFinsh is null\n");
        return false;
    }
//    5.2 调用 onFinsh 方法
    env->CallVoidMethod(g_dsManagerInstance, g_dsFinish);
    return true;
}
